#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 02:32:39 2020

@author: ua17
"""
from load_d import port


def lola_search(port_df, lon, la):
    low = 0
    high = len(port_df) - 1
    while low <= high:
        mid = int((low + high) / 2)
        hit = port_df.iloc[mid]
        if abs(hit.LONGITUDE - lon) <= 0.03 :
            ilocr = range(0,1)
            if hit.LONGITUDE > lon:
                ilocr = range(mid-10, min(mid+3, high))
            else:
                ilocr = range(max(mid-2, low), mid+11)
            ilocr = list(ilocr)
            bool = (abs(port_df.iloc[ilocr, 1] - lon) <= 0.03)  &  (abs(port_df.iloc[ilocr, 2] - la) <= 0.03) 
            return port_df.iloc[ilocr][bool]
        if hit.LONGITUDE > lon:
            high = mid - 1
        else:
            low = mid + 1
            
    return None

lola_search(port, -175.18, -21.14)